/**
 * @Project: Runer
 * @Title: Register.java
 * @Package com.runlooker.runer.register
 * @author town
 * @date 2014-3-27 下午8:20:41
 * @Copyright: 2014 www.yineng.com.cn Inc. All rights reserved.
 * @version V1.0
 */
package com.runlooker.runer.ui.splash;

import org.jivesoftware.smack.XMPPException;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;

import com.runlooker.runer.R;
import com.runlooker.runer.application.Consts;
import com.runlooker.runer.application.MyApplication;
import com.runlooker.runer.interfaces.IConnectionStatusListener;
import com.runlooker.runer.service.XmppService;
import com.runlooker.runer.ui.base.BaseActivity;
import com.runlooker.runer.ui.home.HomeActivty;
import com.runlooker.runer.ui.login.LoginActivity;
import com.runlooker.runer.util.L;
import com.runlooker.runer.util.MeasureUtil;
import com.runlooker.runer.util.PreferenceUtils;

/**
 * @ClassName Register
 * @Description 模块描述
 * @author town
 * @Date 2014-3-27
 */
public class SplashActivity extends BaseActivity implements
		IConnectionStatusListener {
	private static final String tag = SplashActivity.class.getSimpleName();
	public static final String AUTO_LOGIN_ACTION = "auto_login_action";
	private XmppService mXmppService;
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
			try {
				Thread.sleep(3000);
			} catch (InterruptedException e) {
				e.printStackTrace();
			}
			Intent intent = new Intent(SplashActivity.this, HomeActivty.class);
			intent.setAction(AUTO_LOGIN_ACTION);
			startActivity(intent);
			finish();
		}
	};

	ServiceConnection mConnection = new ServiceConnection() {
		@Override
		public void onServiceConnected(ComponentName name, IBinder service) {
			mXmppService = ((XmppService.XMPPBinder) service).getService();
			initialize();
		}

		@Override
		public void onServiceDisconnected(ComponentName arg0) {
			mXmppService = null;
		}
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_splash);
		PreferenceUtils.setPrefInt(this, Consts.SCREEN_WIDTH,
				MeasureUtil.getScreenWidth(this));
		PreferenceUtils.setPrefInt(this, Consts.SCREEN_HEIGHT,
				MeasureUtil.getScreenHeight(this));
		Intent intent = new Intent(SplashActivity.this, XmppService.class);
		bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
	}

	private void initialize() {
		if (!MyApplication.isAccountConfigured(this)) {
			mHandler.postDelayed(gotoLoginAct, 3000);
			L.i(tag, "主动");
		} else {
			new Thread(AutoLoginAct).start();
			L.i(tag, "自动");
		}
	}

	Runnable gotoLoginAct = new Runnable() {
		@Override
		public void run() {
			Intent intent = new Intent(SplashActivity.this, LoginActivity.class);
			startActivity(intent);
			finish();
		}
	};

	Runnable AutoLoginAct = new Runnable() {
		@Override
		public void run() {
			try {
				mXmppService.login();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHandler.sendEmptyMessage(0);
		}
	};

	@Override
	protected void onDestroy() {
		super.onDestroy();
		unbindService(mConnection);
		mHandler.removeCallbacksAndMessages(null);
	}

	@Override
	public void connectionStatusChanged(int connectedState, String reason) {
		// TODO Auto-generated method stub
		if (connectedState == XmppService.DISCONNECTED) {
			try {
				mXmppService.logout();
			} catch (XMPPException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			mHandler.post(gotoLoginAct);
		}

	}
}
